package com.heaven.customerServer.customerController;

import com.heaven.core.entity.DanmuEntity;
import com.heaven.service.DanmuService;
import com.heaven.customerServer.customerController.vo.danmu.SendDanmuReq;
import com.heaven.utils.R;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// dplayer 弹幕接口 heaven-server/danmu/v3 v3必须包括
@RequestMapping("heaven-server/danmuku")
public class DanmuController {
    @Autowired
    protected DanmuService danmuService;
    @Resource
    private LoginController loginController;

    @GetMapping("/load")
    public R getDanmuPool(@RequestParam("videoId") String videoId) {
        if(videoId == null || videoId.isEmpty()) {
            return R.error("视频id不能为空");
        }
        List<DanmuEntity> ans = danmuService.loadArtPlayerDanmuPool(videoId);
        return R.okData(ans);
    }

    // TODO 该接口必须要校验登录状态
    @PostMapping("/auth/send")
    public R sendDanmu(@RequestBody @Valid SendDanmuReq req) {
        boolean flag = danmuService.saveDanmu(req, loginController.getUserSession().getUserId());
        if (!flag) {
            return R.error("发送弹幕失败");
        }
        return R.ok();
    }
}
